<template>
  <div>
    <div>
      <ele-page
        v-loading="loading"
        :formConfig="searchConfig"
        ref="eleTable"
        :operateList="operateList"
        @handleOperateEvent="handleOperateEvent"
        :column="column"
        :getList="getList"
        uniqueId="mesOfflineInspectionItemTable"
      >
      </ele-page>
    </div>
    <ele-form-drawer
      class="dialog-pack"
      :isLoading="isLoading"
      v-model="formDataDialog"
      v-bind="formDescDialog"
      :request-fn="handleSubmit"
      :visible.sync="visible"
      :title="title"
    >
    </ele-form-drawer>
      <!-- 导入弹框 -->
      <ele-form-drawer
              size="30%"
              :isLoading="isLoadingImport"
              v-model="importData"
              v-bind="importFormDescDialog"
              :request-fn="handleSubmitImport"
              :visible.sync="importVisible"
              :destroyOnClose="true"
              title="导入用户"
      >
          <template v-slot:file="{ desc, data, field, formData }">
              <!-- 插槽内容有el-form-item组件包裹 -->
              <uploadNoAuto @onchange="upLoadHandleChange" ref="uploadNoAuto"/>
          </template>
      </ele-form-drawer>
  </div>
</template>

<script>
import {
  operateList,
  searchConfig,
  importFormDescDialog,
  formDescDialog,
  rowConfig,
} from './config'
import {
  getList,getCategoryList,
  rowCreate,
  rowUpdate,
  rowDel,
  rowDetail,
  importData,
} from '@/api/inspection/mesOfflineInspectionItem'
import uploadNoAuto from "@/components/upload-no-auto";

export default {
  name: "mesOfflineInspectionItem",
  components:{
    uploadNoAuto
  },
  data() {
    return {
      title: null,
      visible: false,
      isLoading: false,
      loading: false,
      formDataDialog: {},
      formDescDialog, //新增修改框
      searchConfig,
      operateList,
      getList,
      isLoadingImport: false,  // 导入数据loading
      importData: {},  //导入数据
      importVisible: false,
      importFormDescDialog,  // 导入字段配置
      // 列表页字段的绑定配置
      column: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: "序号",
          fixed: "left",
        },
        {
          prop: "category",
          label: "分类",
        },
        {
          prop: "inspectionType",
          label: "检验类型",
        },
        {
          prop: "inspectionSection",
          label: "检验部位",
        },
        {
          prop: "projectType",
          label: "项目类型",
        },
        {
          prop: "inspectionMethod",
          label: "检验方法",
        },
        {
          prop: "inspectionRequirements",
          label: "检验要求", width: 200,
        },
        {
          prop: "remark",
          label: "备注",
        },
        {
          prop: "createByName",
          label: "创建人名称",
        },
        {
          prop: "createTime",
          label: "创建时间",
          width: 180,
        },
        {
          prop: "updateByName",
          label: "修改人名称",
        },
        {
          prop: "updateTime",
          label: "修改时间",
          width: 180,
        },
        {
          prop: "operate",
          label: "操作",
          width: 160,
          fixed: "right",
          render: (h, scope) => {
            return (
              <ele-row-operation
                scope={scope}
                rowConfig={rowConfig}
                onRowOperate={this.rowOperate}
              />
            )
          },
        },
      ],
    }
  },
  computed: {
  },
  methods: {
    //表格顶部按钮操作事件
    handleOperateEvent(data) {
      this.title = data.name
      const fnc = {
        add: () => {
          this.disableFields(false)
          this.formDataDialog = {}
          this.visible = true
        },
        deleteBatch: () => {
          this.$confirm("此操作将批量删除下线质检项, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            const ids = data.rows.map((val) => val.id).join(',');
            this.rowDel(ids);
          });
        },
        import: () => {
          this.importDialogData = {};
          this.importVisible = true;
        }
      }
      fnc[data.handleName]()
    },
    //表格行操作事件
    rowOperate({ handleName, name, row }) {
      this.title = name
      const fnc = {
        edit: () => {
          this.disableFields(false)
          this.rowDetail(row.id)
          this.visible = true
        },
        detail: () => {
          this.disableFields(true)
          this.rowDetail(row.id)
          this.visible = true
        },
        delete: () => {
          this.$confirm("此操作将删除该下线质检项, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.rowDel(row.id)
          })
        },
      }
      fnc[handleName]()
    },
    doSubmit({ title, api, data}) {
      this.isLoading = true
      api(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || title)
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    //弹框提交事件
    handleSubmit(data) {
      if (this.isLoading) return // 判断状态
    delete data.createBy // 删除不需要修改的非主键字段
    delete data.createByName // 删除不需要修改的非主键字段
    delete data.createTime // 删除不需要修改的非主键字段
    delete data.updateBy // 删除不需要修改的非主键字段
    delete data.updateByName // 删除不需要修改的非主键字段
    delete data.updateTime // 删除不需要修改的非主键字段
    delete data.activeFlag // 删除不需要修改的非主键字段
      const params = this.formDataDialog.id
        ? { title: '修改成功', api: rowUpdate, data}
        : { title: '添加成功', api: rowCreate, data}
      this.doSubmit(params)
    },
    //删除
    rowDel(data) {
      this.loading = true
      rowDel(data)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || "删除成功!")
          this.refresh()
        })
        .finally(() => {
          this.loading = false
        })
    },
    disableFields(flag) {
      this.formDescDialog.disabled = flag
      this.formDescDialog.isShowSubmitBtn = !flag
    },
    rowDetail(id) {
      rowDetail(id)
        .then((res) => {
          this.formDataDialog = res.data.result;
        })
    },
    //刷新表格数据
    refresh(isReset = true) {
      this.$refs.eleTable.refresh(isReset)
    },
    //数据文件上传
    upLoadHandleChange(file){
      this.$set(this.importData,"file",file)
    },
      // 提交导入信息
    handleSubmitImport () {
      if(!this.importData.file){
          this.$message.error("请上传文件")
          return;
      }
      const formData = new FormData();
      formData.append('file', this.importData.file.get('file'));
      this.isLoadingImport = true;
      importData(formData).then((res) => {
          this.$message.success("导入成功");
          this.isLoadingImport = false;
          this.importVisible = false;
          this.refresh();
      }).catch((error) => {
          console.log("错误",error)
          this.$set(this.importData,"file",null)
      }).finally(() => {
          this.isLoadingImport = false;
          this.$refs.uploadNoAuto.clearFiles()
      })
    },
  },
}
</script>
